package com.hnkywl.sts.dao.jsgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.cggl.Cgdmx;
import com.hnkywl.sts.entity.cggl.Jsd;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-4-23
 * Time: 上午9:08
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class CgjsdDao extends SimpleHibernateTemplate<Jsd, Long> {
    public Page<Jsd> findCgjsdList(Integer zt, String jsdh, Date sqrqStart, Date sqrqEnd, Long glqyId, Long gsId, String gsJc, Long fpzlId, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jsd> page = new Page<Jsd>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (jsdh != null && !jsdh.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jsdh like ? ");
            param.add("%" + jsdh.trim() + "%");
        }
        if (sqrqStart != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sqrq >= ?");
            param.add(sqrqStart);
        }
        if (sqrqEnd != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sqrq <= ?");
            param.add(sqrqEnd);
        }
        if (glqyId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" glqy.id = ? ");
            param.add(glqyId);
        }
        if (zt != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" zt = ? ");
            param.add(zt);
        }
//        if (glqyQc != null && !"".equals(glqyQc)) {
//            if (condition.length() != 0)
//                condition.append(" and ");
//            condition.append(" glqy.qc like ? ");
//            param.add("%" + glqyQc.trim() + "%");
//        }
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.id = ? ");
            param.add(gsId);
        }
        if (gsJc != null && !"".equals(gsJc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.jc like ? ");
            param.add("%" + gsJc.trim() + "%");
        }
        if (fpzlId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" fpzl = ? ");
            param.add(fpzlId);
        }
        if (condition.length() != 0)
            condition.append(" and ");
        condition.append(" type = ? ");
        param.add(0);
        //==============记录数量===============
        hql = "select count(*) from Jsd where sftzd!=1 and deleted!=1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jsd where sftzd!=1 and deleted!=1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition + " order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public String findAllCgjsd(Integer zt, Integer type) {
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();

        if (zt == 0) {
            condition.append(" (zt = '0' or zt='1') ");
        } else {
            condition.append(" zt = ? ");
            param.add(zt);
        }
        condition.append(" and type = ? ");
        param.add(type);
        String hql = "select count(*) from Jsd where deleted!=1  " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = (Long) findUnique(hql, param.toArray());
        return c.toString();
    }

    public List<Map> cgjsdList(Long cgjsdId) {
        Criteria criteria = getSession().createCriteria(Cgdmx.class)
                .createAlias("gg", "gg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jzfs", "jzfs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("cgjsd", "cgjsd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("cgd", "cgd", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("cgd.sl").as("cgd.sl"))
                        .add(Property.forName("cgd.id").as("cgd.id"))
                        .add(Property.forName("cgd.cgdh").as("cgdh"))
                        .add(Property.forName("gg.id").as("wzgg.id"))
                        .add(Property.forName("gg.code").as("wzgg.code"))
                        .add(Property.forName("gg.name").as("wzgg.name"))
                        .add(Property.forName("gg.cz").as("wzgg.cz"))
                        .add(Property.forName("gg.ggxh").as("wzgg.ggxh"))
                        .add(Property.forName("gg.dw").as("wzgg.dw"))
                        .add(Property.forName("cd.id").as("cd.id"))
                        .add(Property.forName("rksl").as("rksl"))
                        .add(Property.forName("sbsl").as("sbsl"))
                        .add(Property.forName("jzfs.id").as("jzfs.id"))
                        .add(Property.forName("jzfs.z").as("jzfs.z"))
                        .add(Property.forName("hsj").as("hsj"))
                        .add(Property.forName("bhsj").as("bhsj"))
                        .add(Property.forName("hse").as("hse"))
                        .add(Property.forName("bhse").as("bhse"))
                        .add(Property.forName("jsbhse").as("je"))
                        .add(Property.forName("jsse").as("se"))
                        .add(Property.forName("jsce").as("ce"))
                        .add(Property.forName("cgjsd.me").as("me"))
                        .add(Property.forName("cgjsd.ddfy").as("ddfy"))
                ).add(Restrictions.eq("cgjsd.id", cgjsdId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }
}
